//
//  TuanGoodsDetailsHeaderView.m
//  Findif
//
//  Created by Mac on 2018/11/22.
//  Copyright © 2018年 Cicel Chen. All rights reserved.
//

#import "TuanGoodsDetailsHeaderView.h"
#import "XHStarRateView.h"
#import <CoreLocation/CoreLocation.h>
#import "AlreadyCollectionViewCell.h"
#import "TuanTableViewCell.h"
#import "ZYBannerView.h"

@interface TuanGoodsDetailsHeaderView ()<UICollectionViewDelegate,UICollectionViewDataSource,UITableViewDelegate,UITableViewDataSource,ZYBannerViewDataSource, ZYBannerViewDelegate>

@property (nonatomic, strong) ZYBannerView *banner;
@property (nonatomic, strong)NSArray * dataArray;
@property (nonatomic, weak)UIImageView * topImageView;
@property (nonatomic, weak)UILabel * shopLabel;
@property (nonatomic, weak)UILabel * distanceLabel;
@property (nonatomic, weak)XHStarRateView * starView;
@property (nonatomic, weak)UILabel * commentLabel;
@property (nonatomic, strong)UILabel * localLabel;
//@property (nonatomic, strong)UILabel * dateLabel;
//@property (nonatomic, strong)UIImageView * dateImageView;
@property (nonatomic, weak)UIButton * dateButton;
@property (nonatomic, strong)UIImageView * localImageView;
@property (nonatomic, strong)UIButton * callButton;
@property (nonatomic, strong)UIButton * msgButton;
@property (nonatomic, strong)UIView * oneLineView;
@property (nonatomic, strong)UIView * twoLineView;
@property (nonatomic, strong)UIImageView * couponImageView;
@property (nonatomic, strong)UILabel * pointsLabel;
@property (nonatomic, strong)UIButton * pointsDetailsButton;
@property (nonatomic, strong)UIButton * joinedDetailsButton;
@property (nonatomic, strong)UIButton * commitDetailsButton;
//@property (nonatomic, strong)UILabel * validDateLabel;
//@property (nonatomic, strong)UIView * threeLineView;
@property (nonatomic, strong)UIImageView * joinImageView;
@property (nonatomic, strong)UILabel * joinLabel;
@property (nonatomic, strong)UIImageView * commmitImageView;
@property (nonatomic, strong)UILabel * titleCommitLabel;
@property (nonatomic, strong)UIView * fourLineView;
@property (nonatomic, strong)UIView * joinView;
@property (nonatomic, weak)UILabel * weekLabel;

@property (nonatomic, weak)UILabel * noDataLabel;
@property (nonatomic, weak)UICollectionView * collectionView;
@property (nonatomic, strong)UICollectionViewFlowLayout *layout;
@property (nonatomic, strong)NSArray * userArray;

@property (nonatomic, weak)UITableView * tableView;
@property (nonatomic, weak)UILabel * showLabel;
@property (nonatomic, copy)NSString * merchants_user_id;

@property (nonatomic, weak)UIImageView * findifImageView;
@property (nonatomic, weak)UILabel * activeTimeLabel;
@property (nonatomic, assign)int is_official;

//@property (nonatomic, copy)NSString * merchants_goods_max_pople;
//@property (nonatomic, copy)NSString * merchants_goods_min_pople;
@property (nonatomic, strong)NSDictionary * peopleDict;

@end

@implementation TuanGoodsDetailsHeaderView

- (instancetype)initWithFrame:(CGRect)frame{
    
    if ([super initWithFrame:frame]) {
        
        [self setupUI];
    }
    
    return self;
}


- (void)setDict:(NSDictionary *)dict{

    _dict = dict;
    NSLog(@"dict===%@",dict);
    //    merchants_goods_img
//    NSArray *array = [dict[@"merchants_goods_img"] componentsSeparatedByString:@","];
//    NSString * img = array.firstObject;
//    NSString * usrlStr = [NSString stringWithFormat:@"%@%@",BASEURLSTR,img];
//    NSURL * url = [NSURL URLWithString:usrlStr];
//    [self.topImageView sd_setImageWithURL:url];
    NSString * merchants_goods_max_pople = [NSString stringWithFormat:@"%@",dict[@"merchants_goods_max_pople"]];
    NSString * merchants_goods_min_pople = [NSString stringWithFormat:@"%@",dict[@"merchants_goods_min_pople"]];
    
    self.peopleDict = @{@"merchants_goods_max_pople":merchants_goods_max_pople,@"merchants_goods_min_pople":merchants_goods_min_pople};
    
    self.dataArray = [dict[@"merchants_goods_img"] componentsSeparatedByString:@","];
    self.shopLabel.text = dict[@"merchants_goods_name"];
    int count = [dict[@"merchants_goods_comments_star_level"] intValue];
    NSString * dateStr = [NSString stringWithFormat:@" %@",dict[@"merchants_goods_available_time_now_day"]];
    [self.dateButton setTitle:dateStr forState:UIControlStateNormal];
    self.weekLabel.text = dict[@"merchants_goods_available_time_now_week"];
    if (count == 0) {
        
        self.starView.width = 1;
    }else{
        
        self.starView.currentRating = count;
    }
    
    NSString * lat;
    NSString * lon;
    
    self.is_official = [dict[@"is_official"] intValue];
    if (self.is_official == 0) {

        self.dateButton.hidden = NO;
        self.weekLabel.hidden = NO;
        self.showLabel.hidden = NO;
        self.tableView.hidden = NO;
        self.activeTimeLabel.hidden = YES;
        self.findifImageView.hidden = YES;
        self.findifImageView.width = 0;
        self.localLabel.text = dict[@"merchants_address"];
        
        lat = [NSString stringWithFormat:@"%@",dict[@"merchants_latitude"]];
        lon = [NSString stringWithFormat:@"%@",dict[@"merchants_longitude"]];
    }else{

        self.dateButton.hidden = YES;
        self.weekLabel.hidden = YES;
        self.showLabel.hidden = YES;
        self.tableView.height = 0;
        self.tableView.hidden = YES;
        self.activeTimeLabel.hidden = NO;
        self.findifImageView.hidden = NO;
        self.findifImageView.width = 35 * SCREENH6;
        
        self.activeTimeLabel.text = [NSString stringWithFormat:@"活动时间:%@",dict[@"merchants_goods_field_time"]];
        self.localLabel.text = dict[@"merchants_goods_address"];
        lat = [NSString stringWithFormat:@"%@",dict[@"merchants_goods_address_latitude"]];
        lon = [NSString stringWithFormat:@"%@",dict[@"merchants_goods_address_longitude"]];
    }
    
    self.commentLabel.text = [NSString stringWithFormat:@"%@条评论",dict[@"merchants_goods_comments_number"]];

    if ([self cellHeightWithMsg:self.localLabel.text WithLabel:self.localLabel] > 18) {
        
        self.tableView.y = 435 * SCREENH6;
    }
    
    self.merchants_user_id = [NSString stringWithFormat:@"%@",dict[@"merchants_user_id"]];
    
    double userLat = [DEF_GET_OBJECT(@"lat") doubleValue];
    double userLon = [DEF_GET_OBJECT(@"lon") doubleValue];
    
    if (userLat && userLon) {
        
        double latD = [lat doubleValue];
        double lonD = [lon doubleValue];
        
        double res = [self distanceBetweenOrderBy:latD :userLat :lonD :userLon];
        if (res > 1000) {
            
            res = res / 1000;
            self.distanceLabel.text = [NSString stringWithFormat:@"%.2fkm",res];
        }else{
            
            self.distanceLabel.text = [NSString stringWithFormat:@"%.2fm",res];
        }
    }else{
        
        self.distanceLabel.hidden = YES;
    }
  
    self.userArray = dict[@"AlreadyUser"];
    self.joinLabel.text = [NSString stringWithFormat:@"%lu位用户参加",(unsigned long)self.userArray.count];
    
    if (self.userArray.count > 0) {
        
        self.noDataLabel.hidden = YES;
    }else{
        
        self.noDataLabel.hidden = NO;
    }
    
    NSArray * merchants_goods_available_time_arr = dict[@"merchants_goods_available_time_arr"];
    
    if (merchants_goods_available_time_arr.count == 0) {
        
        self.showLabel.hidden = NO;
    }else{
        
        self.showLabel.hidden = YES;
    }
    self.tuanArray = [NSArray yy_modelArrayWithClass:[ShowTuanWeek class] json:merchants_goods_available_time_arr];
}

- (void)setDataArray:(NSArray *)dataArray{
    
    _dataArray = dataArray;
    
    [self.banner reloadData];
}

- (void)setUserArray:(NSArray *)userArray{
    
    _userArray = userArray;
    
    [self.collectionView reloadData];
}

- (void)setTuanArray:(NSArray<ShowTuanWeek *> *)tuanArray{
    
    _tuanArray = tuanArray;
    
    if (tuanArray.count == 0) {
        
        self.showLabel.hidden = NO;
    }else{
        
        self.showLabel.hidden = YES;
    }
  
    if (self.is_official == 0) {
        
       self.tableView.height = tuanArray.count * 50 * SCREENH6;
    }else{
        
        self.tableView.height = 0;
    }
    
    
    [self.tableView reloadData];
}

- (void)setDateStr:(NSString *)dateStr{
    
    _dateStr = dateStr;
    
    NSString * str = [NSString stringWithFormat:@" %@",dateStr];
    NSString * showStr = [self getTheDayOfTheWeekByDateString:dateStr];
    [self.dateButton setTitle:str forState:UIControlStateNormal];
    self.weekLabel.text = showStr;
}

- (void)setupUI{
    
    self.backgroundColor = ZRWhiteColor;
    self.userArray = [NSArray array];
    
    // 配置banner
    [self setupBanner];
//    UIImageView * topImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,kScreenW,240 * SCREENH6)];
//    
//    topImageView.backgroundColor = [UIColor lightGrayColor];
//    
//    self.topImageView = topImageView;
//    
//    [self addSubview:self.topImageView];
    
    UIImageView * findifImageView = [[UIImageView alloc] initWithFrame:CGRectMake(15 * SCREENH6, 250 * SCREENH6, 0, 15 * SCREENH6)];
    
    findifImageView.image = [UIImage imageNamed:@"官方活动标识"];
    
    self.findifImageView = findifImageView;
    
    [self addSubview:self.findifImageView];
    
    UILabel * shopLabel = [UILabel new];
    
    shopLabel.textColor = ZRBlackColor;
    shopLabel.text = @"王者餐厅";
    shopLabel.font = [UIFont boldSystemFontOfSize:20 * SCREENH6];
    
    self.shopLabel = shopLabel;
    
    [self addSubview:self.shopLabel];
    
    UILabel * distanceLabel = [UILabel new];
    
    distanceLabel.text = @"170m";
    distanceLabel.textColor = [UIColor lightGrayColor];
    distanceLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    
    self.distanceLabel = distanceLabel;
    
    [self addSubview:self.distanceLabel];
    
    
    XHStarRateView *starRateView = [[XHStarRateView alloc] initWithFrame:CGRectMake(15 * SCREENH6, 278 * SCREENH6, 70 * SCREENH6, 10 * SCREENH6)];
    starRateView.isAnimation = YES; // 有动画
    starRateView.rateStyle = XHStarRateViewRateStyeFullStar; //允许不完整星评论
    //    starRateView.delegate = self;
    starRateView.currentRating = 5;
    starRateView.userInteractionEnabled = NO;
    
    self.starView = starRateView;
    [self addSubview:self.starView];
    
    
    UILabel * commentLabel = [UILabel new];
    
    commentLabel.text = @"140条评论";
    commentLabel.textColor = [UIColor lightGrayColor];
    commentLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    
    self.commentLabel = commentLabel;
    
    [self addSubview:self.commentLabel];
    
    self.localImageView = [self setupImageViewImageWithImage:@"place(1)"];
    [self addSubview:self.localImageView];
    self.couponImageView = [self setupImageViewImageWithImage:@"coupon(1)"];
    [self addSubview:self.couponImageView];
    
    self.localLabel = [self setupLabelWithTitle:@"成都剑兰大道29号"];
    self.localLabel.numberOfLines = 2;
    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
    self.localLabel.userInteractionEnabled = YES;
    self.localLabel.textColor = ColorWithRGB(0x4c87d8);
    [self.localLabel addGestureRecognizer:tapGR];
    [self addSubview:self.localLabel];
    self.pointsLabel = [self setupLabelWithTitle:@"优惠券"];
    [self addSubview:self.pointsLabel];
    
    self.callButton = [self setupButtonWithImage:@"phone-receiver"];
    [self addSubview:self.callButton];
    self.msgButton = [self setupButtonWithImage:@"support"];
    [self addSubview:self.msgButton];
    
    self.oneLineView = [self setupLineView];
    [self addSubview:self.oneLineView];
    self.twoLineView = [self setupLineView];
    [self addSubview:self.twoLineView];
//    self.threeLineView = [self setupLineView];
//    [self addSubview:self.threeLineView];
    self.fourLineView = [self setupLineView];
    [self addSubview:self.fourLineView];
    
    //    UILabel * redLabel = [UILabel new];
    //
    //    redLabel.textColor = [UIColor redColor];
    //    redLabel.text = @" -$10 ";
    //    redLabel.font = [UIFont systemFontOfSize:11 * SCREENH6];
    //    redLabel.layer.borderWidth = 0.5;
    //    redLabel.layer.borderColor = [UIColor redColor].CGColor;
    //
    //    self.redLabel = redLabel;
    //
    //    [self addSubview:self.redLabel];
    
    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(15 * SCREENH6, 425 * SCREENH6, 345 * SCREENH6, 100 * SCREENH6) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = ColorWithRGB(0xF4F5F6);
    tableView.rowHeight = 50 * SCREENH6;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.showsVerticalScrollIndicator = NO;

    self.tableView = tableView;

    [self.tableView registerClass:[TuanTableViewCell class] forCellReuseIdentifier:@"TuanGoodsTableViewCell"];

    [self addSubview:self.tableView];
    
    UILabel * showLabel = [UILabel new];
    
    showLabel.text = @"今日暂无场次";
    showLabel.textColor = ZRBlackColor;
    showLabel.font = [UIFont systemFontOfSize:16 * SCREENH6];
    showLabel.hidden = YES;
    
    self.showLabel = showLabel;
    
    [self.tableView addSubview:self.showLabel];
    
    self.pointsDetailsButton = [self setupMoreButtonWithTitle:@"查看详情"];
    [self addSubview:self.pointsDetailsButton];
    self.joinedDetailsButton = [self setupMoreButtonWithTitle:@"查看详情"];
    self.joinedDetailsButton.hidden = YES;
    [self addSubview:self.joinedDetailsButton];
    self.commitDetailsButton = [self setupMoreButtonWithTitle:@"查看更多"];
    [self addSubview:self.commitDetailsButton];

    self.joinLabel = [self setupLabelWithTitle:@"10位用户参加"];
    [self addSubview:self.joinLabel];
    self.titleCommitLabel = [self setupLabelWithTitle:@"评论"];
    [self addSubview:self.titleCommitLabel];
    
    self.joinImageView = [self setupImageViewImageWithImage:@"VectorSmartObject"];
    [self addSubview:self.joinImageView];
    self.commmitImageView = [self setupImageViewImageWithImage:@"图标"];
    [self addSubview:self.commmitImageView];
    
    //获取当前时间日期
//    NSDate *date=[NSDate date];
//    NSDateFormatter *format1=[[NSDateFormatter alloc] init];
//    [format1 setDateFormat:@"yyyy-MM-dd"];
//    NSString *dateStr;
//    dateStr=[format1 stringFromDate:date];
    
    UIButton * dateButton = [UIButton new];
//    NSString * string = [NSString stringWithFormat:@" %@",dateStr];
//    [dateButton setTitle:string forState:UIControlStateNormal];
    [dateButton setTitleColor:ZRBlackColor forState:UIControlStateNormal];
    [dateButton setImage:[UIImage imageNamed:@"calendar"] forState:UIControlStateNormal];
    dateButton.titleLabel.font = [UIFont systemFontOfSize:14 * SCREENH6];
    [dateButton addTarget:self action:@selector(clickPushToCalendarButton) forControlEvents:UIControlEventTouchUpInside];
    
    self.dateButton = dateButton;
    
    [self addSubview:self.dateButton];
    
    UILabel * weekLabel = [UILabel new];
    
    weekLabel.textColor = ZRBlackColor;
    weekLabel.font = [UIFont boldSystemFontOfSize:14 * SCREENH6];
//    NSString * weekString = [self getTheDayOfTheWeekByDateString:dateStr];
//    weekLabel.text = weekString;
    
    self.weekLabel = weekLabel;
    
    [self addSubview:self.weekLabel];
    
    UILabel * label = [UILabel new];
    
    label.text = @"暂无人参与....";
    label.textColor = [UIColor lightGrayColor];
    label.font = [UIFont systemFontOfSize:18 * SCREENH6];
    label.hidden = YES;
    
    self.noDataLabel = label;
    
    [self addSubview:self.noDataLabel];
    //1.初始化layout
    self.layout = [[UICollectionViewFlowLayout alloc] init];
    //设置collectionView滚动方向
    [self.layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    self.layout.itemSize =CGSizeMake(40 * SCREENH6, 45 * SCREENH6);
    self.layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    //2.初始化collectionView
    UICollectionView * collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:self.layout];
    
    collectionView.backgroundColor = [UIColor clearColor];
    collectionView.showsHorizontalScrollIndicator = NO;
    //3.注册collectionViewCell
    //注意，此处的ReuseIdentifier 必须和 cellForItemAtIndexPath 方法中 一致 均为 cellId
    [collectionView registerClass:[AlreadyCollectionViewCell class] forCellWithReuseIdentifier:@"AlreadyCollectionViewCell"];
    //4.设置代理
    collectionView.delegate = self;
    collectionView.dataSource = self;
    //    [collectionView setContentOffset:CGPointMake(-30 * SCREENH6, 0)];
    
    self.collectionView = collectionView;
    
    [self addSubview:self.collectionView];
    
    
    UILabel * activeTimeLabel = [UILabel new];
    
    activeTimeLabel.textColor = [UIColor redColor];
    activeTimeLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    
    activeTimeLabel.hidden = YES;
    
    self.activeTimeLabel = activeTimeLabel;
    
    [self addSubview:self.activeTimeLabel];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return self.tuanArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    TuanTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TuanGoodsTableViewCell" forIndexPath:indexPath];
    
    cell.model = self.tuanArray[indexPath.row];
    cell.dict = self.peopleDict;
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    for (TuanTableViewCell *cell in self.tableView.visibleCells) {
        
        cell.selecedButton.selected = NO;
    }
    
    TuanTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    cell.selecedButton.selected = YES;
    ShowTuanWeek * week = self.tuanArray[indexPath.row];
    
    NSString * time = [NSString stringWithFormat:@"%@",week.time];
    
    NSDictionary * dict = @{@"time":time};
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"updateSelectTime" object:nil userInfo:dict];
}

#pragma mark collectionView代理方法

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return self.userArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    AlreadyCollectionViewCell *cell = (AlreadyCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"AlreadyCollectionViewCell" forIndexPath:indexPath];
    //设置数据
    NSDictionary * dict = self.userArray[indexPath.row];
    
    if([dict[@"head_img"] containsString:@"http"]) {
        
        NSURL * url = [NSURL URLWithString:dict[@"head_img"]];
        
        [cell.imageView sd_setImageWithURL:url];
    } else {
        
        NSString * usrlStr = [NSString stringWithFormat:@"%@%@",BASEURLSTR,dict[@"head_img"]];
        NSURL * url = [NSURL URLWithString:usrlStr];
        
        [cell.imageView sd_setImageWithURL:url];
    }
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    NSDictionary * dict = self.userArray[indexPath.row];
    
    NSString * ID = dict[@"goods_order_user_id"];
    
    NSDictionary * userInfo = @{@"id":ID};
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"pushToPersonal" object:nil userInfo:userInfo];

}

- (void)layoutSubviews{

    [self.shopLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.findifImageView);
        make.leading.mas_equalTo(self.findifImageView.mas_trailing).offset(3 * SCREENH6);
        make.trailing.mas_equalTo(self).offset(-65 * SCREENH6);
    }];
    
    [self.distanceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.shopLabel);
        make.trailing.mas_equalTo(self).offset(-15 * SCREENH6);
    }];
    
    [self.commentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.starView.mas_trailing).offset(5 * SCREENH6);
        make.centerY.mas_equalTo(self.starView);
    }];
    
    [self.msgButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(40 * SCREENH6);
        make.top.mas_equalTo(self.starView.mas_bottom).offset(5 * SCREENH6);
        make.trailing.mas_equalTo(self).offset(-15 * SCREENH6);
    }];
    
    [self.callButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.centerY.mas_equalTo(self.msgButton);
        make.trailing.mas_equalTo(self.msgButton.mas_leading).offset(-20 * SCREENH6);
    }];
    
    [self.localImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.starView);
        make.centerY.mas_equalTo(self.msgButton);
        make.width.mas_equalTo(13 * SCREENH6);
        make.height.mas_equalTo(16 * SCREENH6);
    }];
    
    [self.localLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.localImageView);
        make.leading.mas_equalTo(self.localImageView.mas_trailing).offset(5 * SCREENH6);
        make.trailing.mas_equalTo(self.callButton.mas_leading).offset(-5 * SCREENH6);
    }];
    
    [self.oneLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.localImageView);
        make.trailing.mas_equalTo(self);
        make.height.mas_equalTo(1 * SCREENH6);
        make.top.mas_equalTo(self.localLabel.mas_bottom).offset(13 * SCREENH6);
    }];
    
    [self.couponImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.oneLineView);
        make.top.mas_equalTo(self.oneLineView.mas_bottom).offset(15 * SCREENH6);
        make.width.mas_equalTo(25 * SCREENH6);
        make.height.mas_equalTo(15 * SCREENH6);
    }];
    
    [self.pointsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.couponImageView);
        make.leading.mas_equalTo(self.couponImageView.mas_trailing).offset(5 * SCREENH6);
    }];
    
    //    [self.redLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.centerY.mas_equalTo(self.pointsLabel);
    //        make.leading.mas_equalTo(self.pointsLabel.mas_trailing).offset(5 * SCREENH6);
    //    }];
    
    [self.pointsDetailsButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.pointsLabel);
        make.trailing.mas_equalTo(self).offset(-15 * SCREENH6);
    }];
    
    [self.twoLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.oneLineView);
        make.top.mas_equalTo(self.couponImageView.mas_bottom).offset(15 * SCREENH6);
    }];
    
    [self.dateButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.couponImageView);
        make.top.mas_equalTo(self.twoLineView.mas_bottom).offset(12 * SCREENH6);
    }];
    
    [self.weekLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.centerY.mas_equalTo(self.dateButton);
    }];
    
    [self.showLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.tableView);
        make.centerY.mas_equalTo(self.tableView);
    }];
    
    [self.activeTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.couponImageView);
        make.top.mas_equalTo(self.twoLineView.mas_bottom).offset(12 * SCREENH6);
    }];
    
    [self.joinImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.tableView);
        make.top.mas_equalTo(self.tableView.mas_bottom).offset(12 * SCREENH6);
        make.height.width.mas_equalTo(15 * SCREENH6);
    }];
    
    [self.joinLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.joinImageView);
        make.leading.mas_equalTo(self.joinImageView.mas_trailing).offset(5 * SCREENH6);
    }];
    
    [self.joinedDetailsButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.pointsDetailsButton);
        make.centerY.mas_equalTo(self.joinLabel);
    }];
    
    [self.noDataLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.joinImageView);
        make.top.mas_equalTo(self.joinImageView.mas_bottom).offset(15 * SCREENH6);
    }];
    
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.joinImageView);
        make.top.mas_equalTo(self.joinImageView.mas_bottom).offset(7 * SCREENH6);
        make.height.mas_equalTo(45 * SCREENH6);
        make.trailing.mas_equalTo(self.twoLineView);
    }];
    
    [self.fourLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.twoLineView);
        make.top.mas_equalTo(self.joinImageView.mas_bottom).offset(57 * SCREENH6);
    }];
    
    [self.commmitImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.joinImageView);
        make.top.mas_equalTo(self.fourLineView.mas_bottom).offset(12 * SCREENH6);
        make.width.height.mas_equalTo(15 * SCREENH6);
    }];
    
    [self.titleCommitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.commmitImageView);
        make.leading.mas_equalTo(self.commmitImageView.mas_trailing).offset(5 * SCREENH6);
    }];
    
    [self.commitDetailsButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.joinedDetailsButton);
        make.centerY.mas_equalTo(self.titleCommitLabel);
    }];
}

- (void)setupBanner
{
    // 初始化
    self.banner = [[ZYBannerView alloc] init];
    self.banner.dataSource = self;
    self.banner.delegate = self;
    self.banner.shouldLoop = YES;
    self.banner.autoScroll = YES;
    //    self.banner.pageControlFrame = CGRectMake(200, 195, 45, 32);
    
    [self addSubview:self.banner];
    
    // 设置frame
    self.banner.frame = CGRectMake(0,0,kScreenW,240 * SCREENH6);
}

#pragma mark - ZYBannerViewDataSource
// 返回 Banner 需要显示 Item(View) 的个数
- (NSInteger)numberOfItemsInBanner:(ZYBannerView *)banner
{
    return self.dataArray.count;
}

// 返回 Banner 在不同的 index 所要显示的 View (可以是完全自定义的v iew, 且无需设置 frame)
- (UIView *)banner:(ZYBannerView *)banner viewForItemAtIndex:(NSInteger)index
{
    
    NSString * imageName = self.dataArray[index];
    NSString * usrlStr = [NSString stringWithFormat:@"%@%@",BASEURLSTR,imageName];
    NSURL * url = [NSURL URLWithString:usrlStr];
    
    
    // 创建将要显示控件
    UIImageView *imageView = [[UIImageView alloc] init];
    //    imageView.image = [UIImage imageNamed:imageName];
    [imageView sd_setImageWithURL:url];
    //    [imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"轮播1"]];
    imageView.contentMode = UIViewContentModeScaleAspectFill;
    
    return imageView;
}


- (UIView *)setupJoinView{
    
    UIView * view = [UIView new];
    //
    //    view.backgroundColor = ZRWhiteColor;
    //
    //    int startX = 5 * SCREENH6;
    //    int startY = 5 * SCREENH6;
    //    int widthSpace = 5 * SCREENH6;
    //    int width = 35 * SCREENH6;
    //    int height = 35 * SCREENH6;
    //
    //    NSLog(@"userArray===%@",self.userArray);
    //
    //    for (int i = 0 ; i < 6; i++) {
    ////        NSInteger index = i % 3;
    ////        NSInteger page = i / 3;
    //
    ////        NSDictionary * dict = self.userArray[i];
    //
    ////        NSString * usrlStr = [NSString stringWithFormat:@"%@%@",BASEURLSTR,dict[@"head_img"]];
    ////        NSURL * url = [NSURL URLWithString:usrlStr];
    ////
    //        // 圆角按钮
    //        UIButton * iconBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    //        iconBtn.tag = i;//这句话不写等于废了
    //        iconBtn.frame = CGRectMake(i * (width + widthSpace) + startX, startY, width, height);
    //        [iconBtn setBackgroundColor:ZRBlackColor];
    //        iconBtn.layer.cornerRadius = 17.5 * SCREENH6;
    //        iconBtn.layer.masksToBounds = YES;
    ////        [iconBtn sd_setImageWithURL:url forState:UIControlStateNormal];
    //
    //        //按钮点击方法
    //        [iconBtn addTarget:self action:@selector(iconBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    //
    //        [view addSubview:iconBtn];
    //    }
    
    //    [view layoutIfNeeded];
    
    
    
    
    return view;
}

- (UIButton *)setupMoreButtonWithTitle:(NSString *)title{
    
    UIButton * button = [UIButton new];
    
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(clickDetailButton:) forControlEvents:UIControlEventTouchUpInside];
    button.titleLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    
    return button;
}

- (UILabel *)setupLabelWithTitle:(NSString *)title{
    
    UILabel * label = [UILabel new];
    
    label.text = title;
    label.textColor = ZRBlackColor;
    label.font = [UIFont systemFontOfSize:15];
    
    return label;
}

- (UIImageView *)setupImageViewImageWithImage:(NSString *)image{
    
    UIImageView * imageView = [UIImageView new];
    
    imageView.image = [UIImage imageNamed:image];
    
    return imageView;
}


- (UIButton *)setupButtonWithImage:(NSString *)image{
    
    UIButton * button = [UIButton new];
    
    [button addTarget:self action:@selector(cilckCallButtonOrMesButton:) forControlEvents:UIControlEventTouchUpInside];
    [button setBackgroundColor:ZRBlackColor];
    [button setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
    button.layer.cornerRadius = 20 * SCREENH6;
    button.layer.masksToBounds = YES;
    
    return button;
}

- (UIView *)setupLineView{
    
    UIView * lineView = [UIView new];
    
    lineView.backgroundColor = ColorWithRGB(0xDBE1E7);
    
    return lineView;
}


-(double)distanceBetweenOrderBy:(double) lat1 :(double) lat2 :(double) lng1 :(double) lng2{
    
    CLLocation *curLocation = [[CLLocation alloc] initWithLatitude:lat1 longitude:lng1];
    
    CLLocation *otherLocation = [[CLLocation alloc] initWithLatitude:lat2 longitude:lng2];
    
    double  distance  = [curLocation distanceFromLocation:otherLocation];
    
    return  distance;
    
}

- (void)clickDetailButton:(UIButton *)button{
    
    NSString * type;
    
    if (button == self.joinedDetailsButton) {
        
        type = @"join";
    }else if (button == self.commitDetailsButton){
        
        type = @"commit";
    }else if (button == self.pointsDetailsButton){
        
        type = @"coupon";
    }
    
    NSDictionary * dict = @{@"type":type};
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"clickTuanMoreButton" object:nil userInfo:dict];
}


- (void)cilckCallButtonOrMesButton:(UIButton *)button{
    
    if (button == self.callButton) {
        
        NSMutableString* str=[[NSMutableString alloc] initWithFormat:@"tel:%@",self.dict[@"merchants_phone"]];
        
        UIWebView * callWebview = [[UIWebView alloc] init];
        
        [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
        
        [self addSubview:callWebview];
    }else{
        
        NSString * user_id = DEF_GET_OBJECT(@"id");
        
        if ([user_id isEqualToString:self.merchants_user_id]) {
            
            
            [[NSNotificationCenter defaultCenter] postNotificationName:@"showAlertView" object:nil];
        }else{
            
            [[NSNotificationCenter defaultCenter] postNotificationName:@"PushToChat" object:nil];
        }
    }
}

- (void)clickPushToCalendarButton{
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"pushToCalendarView" object:nil];

}

#pragma mark - TableView 占位图
- (NSString *)xy_noDataViewMessage {
    
    return @"";
}

- (UIColor *)xy_noDataViewMessageColor {
    
    return [UIColor blackColor];
}


//- (void)iconBtnClick:(UIButton *)button{
//    
//    
//}

-(NSString *)getTheDayOfTheWeekByDateString:(NSString *)dateString{
    
    NSDateFormatter *inputFormatter=[[NSDateFormatter alloc]init];
    
    [inputFormatter setDateFormat:@"yyyy-MM-dd"];
    
    NSDate *formatterDate=[inputFormatter dateFromString:dateString];
    
    NSArray *weekdays = [NSArray arrayWithObjects: [NSNull null], @"周日",@"周一", @"周二", @"周三", @"周四", @"周五", @"周六", nil];
    
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierChinese];
    
    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];
    
    [calendar setTimeZone: timeZone];
    
    NSCalendarUnit calendarUnit = NSCalendarUnitWeekday;
    
    NSDateComponents *theComponents = [calendar components:calendarUnit fromDate:formatterDate];
    
    return [weekdays objectAtIndex:theComponents.weekday];
    
}

- (CGFloat)cellHeightWithMsg:(NSString *)msg WithLabel:(UILabel *)label
{
   
    CGSize size = [label sizeThatFits:CGSizeMake(label.width, CGFLOAT_MAX)];
    return size.height;
}

- (void)tapAction{
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"pushToMapView" object:nil];
}


@end
